package com.wjyf.reducing_bento.config;

import com.wjyf.reducing_bento.interceptor.LoginInterceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @Author: lrk
 * @Date: 2023/3/17 下午 10:27
 * @Description:
 */
@Component
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //order指定的是拦截器的顺序
        // 登录拦截器
        registry.addInterceptor(loginInterceptor)
                .excludePathPatterns(
                        "/code/get-code",
                        "/error",
                        "/user-bean/register",
                        "/user-bean/login",
                        "/swagger-resources/**",
                        "/swagger-resources/**/**",
                        "/webjars/**",
                        "/v2/**",
                        "/swagger-ui.html/**",
                        "/api",
                        "/api-docs",
                        "/api-docs/**",
                        "/doc.html/**",
                        "/doc.html",
                        "/api/error",
                        "/favicon.ico",
                        "/service-worker.js",
                        "/img/**"
                ).order(0);
    }

    /**
     * 设置允许跨域
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*");
    }
}
